package com.star.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.star.entity.FriendLink;
import com.star.entity.User;
import com.star.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/admin")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * @Description: 跳转登录页面
     * @Return: 返回登录页面
     */
    @GetMapping
    public String loginPage(){
        return "admin/login";
    }

    /**
     * @Description: 跳转登录页面
     * @Return: 返回登录页面
     */
    @GetMapping("/toIndex")
    public String toIndex(){
        return "admin/index";
    }

    /**
     * @Description: 跳转注册页面
     * @Return: 返回注册页面
     */
    @GetMapping("/toRes")
    public String toRes(){
        return "admin/res";
    }

    /**
     * @Description: 登录校验
     * @Param: username:用户名
     * @Param: password:密码
     * @Param: session:session域
     * @Param: attributes:返回页面消息
     * @Return: 登录成功跳转登录成功页面，登录失败返回登录页面
     */
    @PostMapping("/login")
    public String login(@RequestParam String username,
                        @RequestParam String password,
                        HttpSession session,
                        RedirectAttributes attributes) {
        User user = userService.checkUser(username, password);
        if (user != null) {
            user.setPassword(null);
            session.setAttribute("user",user);
            return "admin/index";
        } else {
            attributes.addFlashAttribute("message", "用户名和密码错误");
            return "redirect:/admin";
        }
    }

    /**
     * @Description: 注销
     * @Param: session:session域
     * @Return: 返回登录页面
     */
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/admin";
    }

    /**
     * 用户注册
     * @param user 用户信息
     * @param file 用户头像
     * @param request 响应
     * @return
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String register(User user, MultipartFile file, HttpServletRequest request, RedirectAttributes attributes){
        Integer res = userService.addUser(user, file, request);
        if (res > 0){
            return "/admin/login";
        }else {
            attributes.addFlashAttribute("message", "请刷新重试");
            return "/admin/res";
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(User user,MultipartFile file,HttpServletRequest request, RedirectAttributes attributes){
        userService.update(user,file,request);
        attributes.addFlashAttribute("message","更新成功");
        return "/admin/index";
    }

    @DeleteMapping(value = "/delete/{id}")
    public String delete(@PathVariable Integer id,RedirectAttributes attributes){
        Integer res = userService.deleteById(id);
        if (res > 0){
            attributes.addFlashAttribute("message","删除成功");
        }else {
            attributes.addFlashAttribute("message","系统繁忙，请重试");
        }
        return "/admin/index";
    }

    @GetMapping("/{id}")
    public String getUserById(@PathVariable Integer id, Model model){
        User user = userService.getById(id);
        model.addAttribute("user",user);
        return "/admin/userInfo";
    }

    @RequestMapping("/getUserList/{pageNum}/{pageSize}")
    public String getUserList(Model model, @PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        if (Objects.isNull(pageNum)){
            pageNum = 10;
        }
        if (Objects.isNull(pageSize)){
            pageSize = 1;
        }
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<User> pageInfo = new PageInfo<>(userService.getList());
        pageInfo.setList(userService.getList());
        model.addAttribute("pageInfo",pageInfo);
        return "/admin/userList";
    }

}